package com.cai;

import com.cai.pojo.User;

import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Title: CollectMain
 * @Author RuoLi
 * @Package com.cai
 * @Date 2024/1/29 10:51
 * @description: 收集
 */
public class CollectMain {
    public static void main(String[] args) {
        Stream<User> stream = Stream.of(
                new User("张三", 18, "男"),
                new User("李四", 20, "男"),
                new User("王五", 18, "女"));

        //把名字映射成名字列表
        List<String> stringList = stream.map(User::getName).collect(Collectors.toList());

        //把名字和性别映射成名字和性别映射的Map
        Map<String, String> stringStringMap = stream.collect(Collectors.toMap(User::getName, User::getSex));

        //把年龄大于18的年龄求和
        long sum = stream.filter(user -> user.getAge() > 18).collect(Collectors.summarizingInt(User::getAge)).getSum();


        String collect = stream.map(User::getSex).collect(Collectors.joining(","));
        System.out.println(collect);//男,男,女

        //分组
        Map<String, List<User>> stringListMap = stream.collect(Collectors.groupingBy(User::getSex));
        System.out.println(stringListMap);//{女=[User(name=王五, age=18, sex=女)], 男=[User(name=张三, age=18, sex=男), User(name=李四, age=20, sex=男)]}


    }
}
